package com.lm.liaoming.http;


import android.content.Context;
import android.util.Log;

import com.tumblr.remember.Remember;

import java.io.IOException;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * 韦敏敏
 */

public class ReceivedCookiesInterceptor implements Interceptor {
    private Context context;

    public ReceivedCookiesInterceptor(Context context) {
        super();
        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {

        Response originalResponse = chain.proceed(chain.request());
        //这里获取请求返回的cookie
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            List<String> stringList = originalResponse.headers("Set-Cookie");
            String cookie = "";
            for (int i = 0; i < stringList.size(); i++) {
                Log.e("=============save","Set-Cookie: " +stringList.get(i));
                String s = stringList.get(i);
                String[] cookieArray = s.split(";");
                cookie = cookieArray[0];
            }
            String[] split = cookie.split("=");
            String cookieName = split[0];
            String cookieValue = split[1];
            Log.e("=======save","cookieName="+cookieName+",,,cookieValue="+cookieValue);
            Remember.putString("cookie_name",cookieName);
            Remember.putString("cookie_value",cookieValue);
        }
        return originalResponse;
    }
}

